<template>
  <div class="app-container">
    <eHeader :param-bean="paramBean" :sup_this="sup_this" />
    <eForm ref="form" :sup_this="sup_this" />
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      ref="mainTable"
      :sup_this="sup_this"
      :data="data.rtnList"
      :total="total"
      @selection-change="handleGridSelectionChange">
      <el-table-column prop="dictType" label="类别编码"/>
      <el-table-column prop="dictCode" label="字典值" />
      <el-table-column prop="dictName" label="名称" />
      <el-table-column prop="sort" label="排序">
        <template slot-scope="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>
      <el-table-column label="语言" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.lan_code" :key="item.id">
            <el-tag v-if="scope.row.lanCode.toString() === item.dictCode" :type="scope.row.lanCode ? '' : 'info'">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="有效" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.enable_flag" :key="item.id">
            <el-tag v-if="scope.row.enableFlag.toString() === item.dictCode" :type="scope.row.enableFlag === 'Y' ? '' : 'info'">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['SYS_ADMIN'])" label="操作" width="130px" align="center">
        <template slot-scope="scope">
          <edit v-permission="['SYS_ADMIN']" :data="scope.row" :sup_this="sup_this"/>
          <el-popover
            v-permission="['SYS_ADMIN']"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定要置为无效数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </DataGridTable>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import eForm from './module/form'
import initData from '@/mixins/initData'
import { del, loadDataUrl } from '@/api/system/dataDict'

export default {
  components: { eHeader, eForm, edit },
  mixins: [initData],
  // 设置数据字典
  dictTypes: ['lan_code', 'enable_flag'],
  data() {
    return {
      delLoading: false,
      sup_this: this
    }
  },
  created() {
    this.url = loadDataUrl()
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async beforeInit() {
      return true
    },
    subDelete(id) {
      this.delLoading = true
      const data = { idList: [id] }
      del(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>
